import { NativeModules, NativeEventEmitter, EmitterSubscription } from 'react-native'

const NativePosModule = NativeModules.PosModule
const NativePosModule_NativeEventEmitter = new NativeEventEmitter(NativePosModule)

export interface PosInfo {
	/**
	 * 设备厂商
	 */
	Vendor: string,
	/**
	 * 硬件序列号号
	 */
	SN: string,
	/**
	 * 设备机型
	 */
	TermType: string,
	/**
	 * 智能桌面版本
	 */
	SoftwareVer: string,

}

export interface IdentifyInfo {
	/**
	 * 姓名
	 */
	name: string,
	/**
	 * 性别
	 */
	sex: string,
	/**
	 * 民族
	 */
	nation: string,
	/**
	 * 出生年月
	 */
	birthday: string,
	/**
	 * 身份证号
	 */
	cardNo: string,
	/**
	 * 签发机关
	 */
	authority: string,
	/**
	 * 地址
	 */
	address: string,
	/**
	 * DN
	 */
	DN: string,
	/**
	 * 有效时间
	 */
	expdate: string,
	/**
	 * 卡片类型
	 */
	cardtype: string,
	/**
	 * 签证次数
	 */
	issueFrequency: string,
	/**
	 * 通行证号
	 */
	passNumber: string,

}

export default class PosModule {
	static rePrint = (traceNo: string): Promise<string> => {
		return NativePosModule.rePrint(traceNo);
	}

	static setId = (id: string): Promise<boolean> => {
		return NativePosModule.setId(id);
	}

	static setAppKey = (appKey: string): Promise<boolean> => {
		return NativePosModule.setAppKey(appKey);
	}

	static pay = (amount: number, extOrderNo: string): Promise<string> => {
		return NativePosModule.pay(amount, extOrderNo)
	}

	static payHj = (posT: boolean, amount: number, extOrderNo: string, name: string, accountNo: string, merchantNo: string): Promise<string> => {
		return NativePosModule.payHj(posT, amount, extOrderNo, name, accountNo, merchantNo)
	}

	static payHjTest = (posT: boolean, payJson: string): Promise<string> => {
		return NativePosModule.payHjTest(posT, payJson)
	}

	static payQRCode = (amount: number, extOrderNo: string): Promise<string> => {
		return NativePosModule.payQRCode(amount, extOrderNo)
	}

	static cancelOrder = (orgTraceNo: string): Promise<string> => {
		return NativePosModule.cancelOrder(orgTraceNo)
	}

	static cancelOrderHj = (posT: boolean, orgTraceNo: string): Promise<string> => {
		return NativePosModule.cancelOrderHj(posT, orgTraceNo)
	}

	static sign = (): Promise<string> => {
		return NativePosModule.sign();
	}

	static balance = (): Promise<string> => {
		return NativePosModule.balance();
	}

	static balanceYe = (): Promise<string> => {
		return NativePosModule.balanceYe();
	}

	static callTrans = (transAppName: string, transAppId: string, transDataJson: string): Promise<string> => {
		return NativePosModule.callTrans(transAppName, transAppId, transDataJson);
	}

	static getPosInfo = (): Promise<PosInfo> => {
		return NativePosModule.getPosInfo();
	}

	static print = (base64: string): Promise<boolean> => {
		return NativePosModule.print(base64)
	}

	static printText = (text: string): Promise<boolean> => {
		return NativePosModule.printText(text)
	}

	static setupCardReader = (data: { loginAddress: string, md5Key: string }): Promise<boolean> => {
		return NativePosModule.setupCardReader(data)
	}

	static readCardInfo = (readBegin: (msg: string) => void, readFail: (err: string) => void): Promise<IdentifyInfo> => {
		const readCallback = NativePosModule_NativeEventEmitter.addListener('readBegin', readBegin);
		const readFailCallback = NativePosModule_NativeEventEmitter.addListener('readFail', readFail);
		return NativePosModule.readCardInfo()
			.then((res: IdentifyInfo) => {
				readCallback.remove();
				readFailCallback.remove();
				return res
			})
	}

	static stopReadCard = (): Promise<boolean> => {
		return NativePosModule.stopReadCard()
	}

	static settings = (settingType: int): Promise<boolean> => {
		return NativePosModule.settings(settingType)
	}

	static autoSelfUpdate = (): Promise<string> => {
		return NativePosModule.autoSelfUpdate()
	}

	static queryOrder = (extOrderNo: string): Promise<string> => {
		return NativePosModule.queryOrder(extOrderNo)
	}
}
